﻿using System;
using System.Collections.Generic;
using System.Text;
using IDAL.RF.Repair;
using Model.RF.Repair;

namespace BLL.RF.Repair
{
    public class RepairLock : BaseService<RepairLockInfo>
    {
        private static readonly IRepairLock dal = DALFactory.DataAccess.CreateRepairLock();

        public override void SetCurrentRepository()
        {
            CurrentRepository = dal;
        }

        public bool RepairLock_AutoBuild(RepairLockInfo rli)
        {
            return Insert(rli);
        }

        public bool RepairLock_AutoUnlock(RepairLockInfo rli)
        {
            try
            {
                Update(rli);
                return true;
            }
            catch
            {
                throw;
            }
        }

        public bool RepairLock_ProductSNIsLock(string productSN)
        {
            IList<RepairLockInfo> list = RepairLock_GetValidbyProductSN(productSN);
            if (list.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool RepairLock_ProductSNIsLock(string productSN, string stepItem)
        {
            IList<RepairLockInfo> list = RepairLock_GetValidbyProductSN(productSN);
            foreach (RepairLockInfo rli in list)
            {
                if (rli.StepItem.Equals(stepItem,StringComparison.OrdinalIgnoreCase))
                    return true;
            }
            return false;
        }

        public RepairLockInfo RepairLock_GetLatestValidbyProductSN(string productSN)
        {
            IList<RepairLockInfo> list = RepairLock_GetValidbyProductSN(productSN);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new RepairLockInfo();
            }
        }

        public IList<RepairLockInfo> RepairLock_GetValidbyProductSN(string productSN)
        {
            IList<RepairLockInfo> list = new List<RepairLockInfo>();
            foreach (RepairLockInfo rli in RepairLock_GetbyProductSN(productSN))
            {
                if (rli.IsValid)
                    list.Add(rli);
            }
            return list;
        }

        public IList<RepairLockInfo> RepairLock_GetbyProductSN(string productSN)
        {
            return dal.RepairLock_GetbyProductSN(productSN);
        }

        public IList<RepairLockInfo> RepairLock_GetbyDT(DateTime dt1, DateTime dt2)
        {
            return dal.RepairLock_GetbyDT(dt1, dt2);
        }
    }
}
